package com.simon.app.october.modules.sys.controllers;

import com.simon.app.october.common.annotation.SysLog;
import com.simon.app.october.common.utils.Result;
import com.simon.app.october.common.validator.ValidatorUtils;
import com.simon.app.october.common.validator.group.AddGroup;
import com.simon.app.october.common.validator.group.UpdateGroup;
import com.simon.app.october.modules.sys.entity.SysMenuEntity;
import com.simon.app.october.modules.sys.service.ShiroService;
import com.simon.app.october.modules.sys.service.SysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {

    @Autowired
    SysMenuService menuService;

    @Autowired
    ShiroService shiroService;

    @GetMapping("/nav")
    public Result nav() {

        // 先是获取menu数据
        List<SysMenuEntity> menuList = menuService.getUserMenuList(getUserId());

        // 再获取权限
        Set<String> permissions = shiroService.getUserPermissions(getUserId());

        return Result.ok().put("menuList", menuList).put("permissions", permissions);
    }

    /**
     * 这个接口我就不想吐槽了，源码作者估计瓦特了，居然不走统一的返回结构，突然觉得这个作者好像也不咋地
     * 怎么办，有种被坑的感觉！
     * */
    @GetMapping("/list")
    public List<SysMenuEntity> list() {
        return menuService.getList();
    }

    @GetMapping("/select")
    public Result select() {
        // 获取所有菜单
        List<SysMenuEntity> list = menuService.getNotButtonMenuList();

        // 添加一个顶级菜单
        SysMenuEntity top = new SysMenuEntity();
        top.setName("一级菜单");
        top.setMenuId(0L);
        top.setParentId(-1L);
        list.add(top);

        return Result.ok().put("menuList", list);
    }

    @GetMapping("/info/{menuId}")
    public Result info(@PathVariable("menuId") Long menuId) {
        return Result.ok().put("menu", menuService.findById(menuId));
    }

    /**
     * 更新菜单，当然这里面我没做很细的参数验证，比如父级菜单只能是目录而不是按钮
     * */
    @PostMapping("/update")
    @RequiresPermissions("sys:menu:update")
    @SysLog("更新菜单")
    public Result update(@RequestBody SysMenuEntity menuEntity) {
        ValidatorUtils.validateEntity(menuEntity, UpdateGroup.class);
        menuService.update(menuEntity);
        return Result.ok();
    }

    @PostMapping("/save")
    @RequiresPermissions("sys:menu:save")
    @SysLog("新增菜单")
    public Result save(@RequestBody SysMenuEntity menuEntity) {
        ValidatorUtils.validateEntity(menuEntity, AddGroup.class);
        menuService.save(menuEntity);
        return Result.ok();
    }

    @PostMapping("/delete/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    @SysLog("删除菜单")
    public Result delete(@PathVariable("menuId") long menuId) {

        // 系统菜单不能删除
        if (31L > menuId) {
            return Result.error(500,"系统菜单不能删除");
        }

        // 有子菜单不能删除
        List<SysMenuEntity> list = menuService.getListByParentId(menuId);
        if (list.size() > 0) {
            return Result.error(500, "请先删除所有子菜单！");
        }

        menuService.delete(menuId);
        return Result.ok();
    }
}
